﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using DTO;

namespace DAO
{
    public class HangXeDAO
    {
        public static List<HangXeDTO> LayDanhSach()
        {
            List<HangXeDTO> lstHangXe = null;
            try
            {
                OleDbConnection cnn = BoKetNoiCSDL.KetNoi();
                OleDbCommand cmd = new OleDbCommand("SELECT * FROM HANG_XE", cnn);
                lstHangXe = new List<HangXeDTO>();

                try
                {
                    OleDbDataReader rd = cmd.ExecuteReader();
                    while(rd.Read())
                    {
                        HangXeDTO hangXe = new HangXeDTO();
                        hangXe.MaHangxe = (int)rd["MaHangXe"];
                        hangXe.TenHangXe = (string)rd["TenHangXe"];
                        lstHangXe.Add(hangXe);
                    }
                }
                catch (System.Exception ex)
                {
                	if(cnn.State == ConnectionState.Open)
                    {
                        cnn.Close();
                    }
                    throw ex;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return lstHangXe;
        }

        public static int Them(HangXeDTO hangXeDto)
        {
            int result = 0;
            try
            {
                OleDbConnection cnn = BoKetNoiCSDL.KetNoi();
                string cmdText = String.Format("INSERT INTO HANG_XE VALUES(N'{0}')", hangXeDto.TenHangXe);
                OleDbCommand cmd = new OleDbCommand(cmdText, cnn);
                cnn.Open();
                try
                {
                    result = cmd.ExecuteNonQuery();
                    cnn.Close();
                }
                catch (System.Exception ex)
                {
                	if(cnn.State == ConnectionState.Open)
                    {
                        cnn.Close();
                    }
                    throw ex;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
    }
}
